<template>
  <a-modal
    title="操作"
    :width="800"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item :label-col="labelCol" :wrapper-col="wrapperCol" label="服务商">
          <a-select :disabled="display"
            v-decorator="[ 'serviceBusinessId', {rules: [{ required: true, message: '请选择服务商' }],initialValue: defaultCompany}]"
            placeholder="请选择服商户">
            <a-select-option :value="company.id" v-for="company in companyList" :key="company.id">
              {{company.name}}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分账接收方全称"
        >
          <a-input placeholder="商户微信注册全称或个人微信实名认证名字"
                   v-decorator="[ 'name', {rules: [{ required: true, message: '请输入。。。' }]} ]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分账接收方类型"
        >
          <a-select
            v-decorator="[ 'type', {rules: [{ required: true, message: '请选择服务商' }]}]"
            placeholder="分账接收方类型">
            <a-select-option value="MERCHANT_ID" key="MERCHANT_ID">商户号</a-select-option>
            <a-select-option value="PERSONAL_WECHATID" key="PERSONAL_WECHATID">个人微信号</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="分账接收方账号"
        >
          <a-input placeholder="微信商户号或个人微信号" v-decorator="[ 'account', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="与分账方的关系类型"
        >
          <a-select
            v-decorator="[ 'relation_type', {rules: [{ required: true, message: '与分账方的关系类型' }]}]"
            placeholder="与分账方的关系类型">
            <a-select-option value="SERVICE_PROVIDER" key="SERVICE_PROVIDER">服务商</a-select-option>
            <a-select-option value="STORE" key="STORE">门店</a-select-option>
            <a-select-option value="STAFF" key="STAFF">员工</a-select-option>
            <a-select-option value="STORE_OWNER" key="STORE_OWNER">店主</a-select-option>
            <a-select-option value="PARTNER" key="PARTNER">合作伙伴</a-select-option>
            <a-select-option value="HEADQUARTER" key="HEADQUARTER">总部</a-select-option>
            <a-select-option value="BRAND" key="BRAND">品牌方</a-select-option>
            <a-select-option value="DISTRIBUTOR" key="DISTRIBUTOR">分销商</a-select-option>
            <a-select-option value="USER" key="USER">用户</a-select-option>
            <a-select-option value="SUPPLIER" key="SUPPLIER">供应商</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="具体关系"
        >
          <a-input placeholder="具体关系" v-decorator="[ 'custom_relation', {rules: [{ required: true, message: '请输入。。。' }] }]"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { save} from '@/api/receiverService/receiver'
  import { selectAll} from '@/api/businessService/business'
  import pick from 'lodash.pick'

  export default {
    //--############  data() ###############-->
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        visible: false,
        confirmLoading: false,
        mdl: {},
        form: this.$form.createForm(this),
        companyList:[],
        defaultCompany:[],
        isDisabled: false,
        display:false
      }
    },
    //--############  data() ###############-->

    created() {
      selectAll().then(res => {
        this.companyList = res.result
        this.defaultCompany = this.companyList[0].id
        this.company = {
          'companyId': this.defaultCompany,
          'sourceType': 'bigWheel'
        }

      })
    },

    //--############  methods() ###############-->
    methods: {
      add() {
        this.isDisabled = false
        this.visible = true
      },
      edit(record) {
        this.display = true
        this.isDisabled = true
        this.mdl = Object.assign({}, record)
        if(record.status){
          this.mdl.status = 1
        }else{
          this.mdl.status = 0
        }
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl, 'name', 'type', 'account','relation_type','custom_relation'))
        })
      },
      close() {
        this.$emit('close')
        this.visible = false
        this.form.resetFields()
        this.mdl = {}
      },
      handleOk() {
        const _this = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          // 验证表单没错误
          if (!err) {
            values.id = this.mdl.id
            _this.confirmLoading = true
            save(values).then((res) => {
              if (res.code === 200) {
                _this.$message.success('保存成功')
                _this.$emit('ok')
                _this.close()
              } else {
                this.$notification.error({
                  message: '错误',
                  description: res.message,
                  duration: 4
                })
              }
            }).catch(() => {
              // Do something
            }).finally(() => {
              _this.confirmLoading = false
            })
          }
        })
      },
      handleCancel() {
        this.close()
      }
    }
    //--############  methods() ###############-->
  }
</script>

<style scoped>

</style>
